import validateForm from "../../utils/validateForm";
Page({
  data: {
    userInfo: {},
    careerArr: ["国企事业单位", "外资", "私企", "个体"],
    emotionalStateArr: ["已婚", "单身", "恋爱中", "离异", "丧偶", "其他"],
    haveChildArr: ["没有", "一个孩子", "一个以上", "其他"],
    group: [
      {
        label: "父母",
        value: "parent",
      },
      {
        label: "夫妻",
        value: "spouse",
      },
      {
        label: "子女",
        value: "child",
      },
      {
        label: "恋人",
        value: "lover",
      },
      {
        label: "朋友",
        value: "friend",
      },
      {
        label: "同事",
        value: "colleague",
      },
      {
        label: "其他",
        value: "other",
      },
    ],
    sexGroup: [
      {
        label: "男",
        value: "male",
      },
      {
        label: "女",
        value: "female",
      },
    ],
    rules: {
      name: [{ required: true, message: "请填写用户名" }],
      birth: [{ required: true, message: "请填写生日" }],
      gender: [{ required: true, message: "请选择性别" }],
      phone: [{ required: true, message: "请填写手机号" }],
      contactName: [{ required: true, message: "请填写紧急联系人姓名" }],
      contractRela: [{ required: true, message: "请填写与紧急联系人关系" }],
      contractPhone: [{ required: true, message: "请填写紧急联系人手机号" }],
      //   career: [{ required: true, message: "请选择您的职业" }],
      //   city: [{ required: true, message: "请选择您的现居地址" }],
      //   emotionalState: [{ required: true, message: "请选择您的情感状态" }],
      //   haveChild: [{ required: true, message: "请选择是否有孩子" }],
    },
    errMsg: "",
    checkAgrrent: false,
    isEdit: false,
    id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    console.log(option, "option");
    if (option.id) {
      this.setData({
        isEdit: true,
        id: option.id,
      });
      this.queryInfo(option.id);
    }
  },
  async queryInfo(id) {
    try {
      const res = await getApp().request({
        path: "/s/v/getVisitor",
        data: {
          id,
        },
      });
      const { birth } = res;
      const birthStr = birth.toString();
      const year = birthStr.substring(0, 4);
      const month = birthStr.substring(4, 6);
      const day = birthStr.substring(6, 8);
      const birthDate = `${year}-${month}-${day}`;
      res.birth = birthDate;
      this.setData({
        userInfo: res,
      });
    } catch {}
  },
  handleFormChange(e) {
    const { key: fieldName } = e.target.dataset;
    const fieldValue = e.detail.value; // 获取字段的新值
    console.log(fieldName, fieldValue, "sda");
    this.setData({
      [`userInfo.${fieldName}`]: fieldValue,
    });
  },
  async onFormSubmit(e) {
    const formData = e.detail.value;
    console.log(formData, "formData");
    validateForm(formData, this.data.rules)
      .then(async () => {
        if (!this.data.checkAgrrent) {
          wx.showToast({
            title: "请勾选相应协议",
            icon: "none",
          });
          return;
        }
        const url = this.data.isEdit ? "/s/v/updateVisitor" : "/s/v/addVisitor";
        const newBirth = Number(formData.birth.split("-").join(""));
        await getApp().request({
          path: url,
          method: "POST",
          data: {
            ...formData,
            birth: newBirth,
            id: this.data.id,
            type: "adult",
          },
        });
        wx.navigateBack({
          delta: 1, // 返回上一级页面
        });
      })
      .catch((err) => {
        console.log(err, "eee");
        const errMsg: any = Object.values(err)[0];
        this.setData({
          errMsg,
        });
      });
  },
  handleCheckChange(e) {
    this.setData({
      checkAgrrent: e.detail.value,
    });
  },
});
